﻿using CallCenter.Data.Base;
using CallCenter.Data.BizUtilities.Base;
using CallCenter.Data.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CallCenter.Data.BizUtilities
{
   public class DepartmentUtility:BaseUtility
    {
        public DepartmentUtility(DataManager manager)
            :base(manager)
        { }

        public ReturnMessage<DepartmentViewModel> InsertToJobTitle(DepartmentViewModel model)
        {
            var returnMessage = new ReturnMessage<DepartmentViewModel>();
            //  if(this.ValidateScratchPadModel(model))
            //   {
            if (String.IsNullOrEmpty(model.description))
                model.description = "";

            var id = this.Insert(model.code, model.description);
             if (id == -1)
            {
                returnMessage.Message = "We cannot insert data to database";
                returnMessage.Status = false;
            }
            else
            {
                model.id = id;
                returnMessage.Status = true;
            }
            return returnMessage;
        }

        public ReturnMessage<DepartmentViewModel> UpdateToJobTitle(DepartmentViewModel model)
        {
            var returnMessage = new ReturnMessage<DepartmentViewModel>();
            //  if(this.ValidateScratchPadModel(model))
            //   {
            var id = this.Update( model.id, model.description,model.code);
            if (id == -1)
            {
                returnMessage.Message = "We cannot insert data to database";
                returnMessage.Status = false;
            }
            else
            {
                model.id = id;
                returnMessage.Status = true;
            }
            return returnMessage;
        }


        public List<DepartmentViewModel> GetAll() //Gets all the data form the contacts table
        {
            string sql = "Select * from ddlDepartments";

            return this.Factory.ExecuteObjects<DepartmentViewModel>(sql);
        }
        public int Insert(string code, string desription) //insert data to a contact table
        {
            int nRtn = -1;
           string sql = "INSERT INTO ddlDepartments (code, description) Values(@code, @desc) SELECT @pResult =@@IDENTITY";


            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@code", SqlDbType.NVarChar, 10, code);
                this.Factory.AddParameter(cmd, "@desc", SqlDbType.NVarChar, 100, desription);

                var param = this.Factory.AddParameter(cmd, "@pResult", System.Data.SqlDbType.NVarChar, 500, null);
                param.Direction = System.Data.ParameterDirection.Output;

                this.Factory.ExecuteNonQuery(cmd);
                nRtn = Convert.ToInt32(param.Value);
            }
            return nRtn;
        }

        public int Update(int id, string desc, string code)
        {
            int nRtn = -1;
            string sql  = "UPDATE ddlDepartments " + "SET " +  " code=@code, description=@desc WHERE id=@id";

            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@code", SqlDbType.NVarChar, 10, code);
                this.Factory.AddParameter(cmd, "@desc", SqlDbType.NVarChar, 100, desc);

                this.Factory.AddParameter(cmd, "@id", SqlDbType.Int, 10, id);
                this.Factory.ExecuteNonQuery(cmd);

            }
            nRtn = id;
            return nRtn; //retruning the data stored in the table
        }

    

        public void Delete(int nAddressType) //delete the row of data using the id passed to the function
        {
           string sql= "DELETE FROM ddlDepartments " +
                "WHERE ID = @ID ";

        using (var cmd = this.Factory.GetCommand(sql))
        {
            this.Factory.AddParameter(cmd, "@ID", SqlDbType.Int, 10, nAddressType);
            this.Factory.ExecuteNonQuery(cmd);
        }

    }


    public void DeleteAll() //delete all the rows in the data.
        {
           string sql = "DELETE FROM ddlDepartments ";
            this.Factory.ExecuteTable(sql);

        }

    }
}
